package com.ysyc.driver_ee.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.api.HttpResource;
import com.ysyc.driver_ee.api.NetworkCallback;
import com.ysyc.driver_ee.api.RetrofitClient;
import com.ysyc.driver_ee.base.BaseActivity;
import com.ysyc.driver_ee.constant.HawkKeys;
import com.ysyc.driver_ee.model.DriverInfo;
import com.ysyc.driver_ee.model.UserInfo;
import com.ysyc.driver_ee.tools.ToastUtils;
import com.ysyc.driver_ee.widget.TitleView;
import com.ysyc.driver_ee.widget.VerifyCodeView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/11/1 9:10<br>
 * 版本： v1.0<br>
 */
public class VerificationCodeActivity extends BaseActivity {

    @BindView(R.id.title_verification_view)
    TitleView titleVerificationView;
    @BindView(R.id.tv_phone_view)
    TextView tvPhoneView;
    @BindView(R.id.tv_verificationCode_view)
    VerifyCodeView tvVerificationCodeView;
    @BindView(R.id.tv_send_msg_view)
    TextView tvSendMsgView;
    private Disposable timeSubscribe;
    private int time = 10;
    private NetworkCallback<UserInfo> userInfoNetworkCallback;
    private NetworkCallback<DriverInfo> tNetworkCallback;
    public final static String DRIVER_INFO ="DriverInfo";



    @Override
    protected int getLayoutId() {
        return R.layout.activity_verification_code;
    }

    @Override
    protected void initView() {
        titleVerificationView.setLeftImg(R.drawable.left_gray_return_icon);
        titleVerificationView.setLeftImgClick(v -> finish());
        titleVerificationView.setTitleViewBackground(R.color.white_ff);
        String phoneNumber = getIntent().getStringExtra(LoginActivity.PHONE_NUMBER);
        tvPhoneView.setText(" +86 " + phoneNumber);
        countdown();
        initNetworkCallback();
        tvVerificationCodeView.setInputCompleteListener(new VerifyCodeView.InputCompleteListener() {
            @Override
            public void inputComplete(String inputContent) {
                Observable<UserInfo> userInfoObservable = RetrofitClient.getInstance().getHttpServices().startOrders(phoneNumber.replace(" ", ""), inputContent);
                userInfoNetworkCallback.postRequest(userInfoObservable, null);
            }

            @Override
            public void invalidContent() {

            }
        });
    }

    private void initNetworkCallback() {
        userInfoNetworkCallback = new NetworkCallback<>(new HttpResource<UserInfo>() {
            @Override
            public void onSuccess(UserInfo userInfo) {
                dismissDilog();
                Hawk.put(HawkKeys.TOKEN_KEY, userInfo.token);
                Hawk.put(HawkKeys.ALIAS, userInfo.alias);
                Observable<DriverInfo> driverInfoObservable = RetrofitClient.getInstance().getHttpServices().obtainDriverInfo();
                tNetworkCallback.postRequest(driverInfoObservable, null);
            }

            @Override
            public void onFailure(String errorDes) {
                ToastUtils.show(errorDes);

            }

            @Override
            public void onLoading(Dialog dialog) {
                showLoadingDialog();
            }
        });

        tNetworkCallback = new NetworkCallback<>(new HttpResource<DriverInfo>() {
            @Override
            public void onSuccess(DriverInfo driverInfo) {
                dismissDilog();
                ToastUtils.show("登录成功");
                Intent intent = new Intent(VerificationCodeActivity.this, MainActivity.class);
                intent.putExtra(DRIVER_INFO,driverInfo);
                startActivity(intent);
            }

            @Override
            public void onFailure(String errorDes) {
                dismissDilog();

            }

            @Override
            public void onLoading(Dialog dialog) {

            }
        });


    }


    private void countdown() {
        timeSubscribe = Observable.interval(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> {
                    time = time - 1;
                    tvSendMsgView.setText(time + "秒后重新获取验证码");
                    if (0 == time) {
                        onDestroyDispose();
                        sendMsgCode();
                    }
                });
    }

    private void sendMsgCode() {
        tvSendMsgView.setText("获取验证码");
        tvSendMsgView.setTextColor(Color.parseColor("#FF41C659"));
        tvSendMsgView.setOnClickListener(v -> {
            if (0 == time) {
                ToastUtils.show("验证码发送成功");
                time = 10;
                tvSendMsgView.setTextColor(Color.parseColor("#ff666666"));
                countdown();
            } else {
                ToastUtils.show(time + "秒后重新获取验证码");
            }

        });


    }

    private void onDestroyDispose() {
        if (timeSubscribe != null && !timeSubscribe.isDisposed()) {
            timeSubscribe.dispose();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onDestroyDispose();
    }
}
